package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;

    /**
     * 条件查询学员信息
     *
     * @param student
     * @return
     */
    @Override
    public PageResult<Student> queryStudent(StudentQueryParam student) {
        //使用Page方法
        Page<Student> p = new Page<>(student.getPage(),student.getPageSize());
        //根据修改时间倒序排序
        p.addOrder(OrderItem.desc("update_time"));
        //根据条件查询
        p = lambdaQuery().like(student.getName() != null && !student.getName().isEmpty(), Student::getName, student.getName())
                .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId())
                .page(p);
        //遍历学员信息
        List<Student> students = p.getRecords();
        //获取到学员所对于的班级id
        List<Long> clazzid = students.stream().map(stu -> stu.getClazzId()).collect(Collectors.toList());
        //判断该学院是否存在班级id
        List<Clazz> clazzes = clazzService.listByIds(clazzid);
        if(!clazzid.isEmpty()){
            //遍历学员信息，在遍历班级名称信息赋给每个学员
            students.forEach(stu -> {
                clazzes.forEach(clazz -> {
                    if (stu.getClazzId() != null && stu.getClazzId().equals(clazz.getId())){
                        stu.setClazzName(clazz.getName());
                    }
                });
            });
        }

        //返回页码，以及每页总记录数
        return new  PageResult<>(p.getTotal(),students);
    }

    /**
     * 新增学员信息
     * @param student
     */
    @Override
    public void saveStudent(Student student) {
        //补充基础代码
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        save(student);
    }

    /**
     * 修改学员信息
     *
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    /**
     * 违纪
     * @param id
     * @param score
     */
    @Override
    public void student(Integer id, Integer score) {
        Student byId = getById(id);
        if(byId.getViolationCount() != null && byId.getViolationScore() != null){
            byId.setViolationCount((short) (byId.getViolationCount()+count()));
            byId.setViolationScore((short) (byId.getViolationScore()+score));
        }

    }
}
